﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Rubik.Common
{
    public class CubeElement
    {
        private readonly byte[] facets = new byte[6];

        public byte Top
        {
            get
            {
                return facets[0];
            }
            set
            {
                facets[0] = value;
            }
        }

        public byte Left
        {
            get
            {
                return facets[1];
            }
            set
            {
                facets[1] = value;
            }
        }

        public byte Front
        {
            get
            {
                return facets[2];
            }
            set
            {
                facets[2] = value;
            }
        }

        public byte Right
        {
            get
            {
                return facets[3];
            }
            set
            {
                facets[3] = value;
            }
        }

        public byte Bottom
        {
            get
            {
                return facets[4];
            }
            set
            {
                facets[4] = value;
            }
        }

        public byte Back
        {
            get
            {
                return facets[5];
            }
            set
            {
                facets[5] = value;
            }
        }

        private void RotateX(bool clockwise)
        {
            if (clockwise)
            {
                byte copy = Top;
                Top = Back;
                Back = Bottom;
                Bottom = Front;
                Front = copy;
            }
            else
            {
                byte copy = Top;
                Top = Front;
                Front = Bottom;
                Bottom = Back;
                Back = copy;
            }
        }

        private void RotateY(bool clockwise)
        {
            if (clockwise)
            {
                byte copy = Front;
                Front = Right;
                Right = Back;
                Back = Left;
                Left = copy;
            }
            else
            {
                byte copy = Front;
                Front = Left;
                Left = Back;
                Back = Right;
                Right = copy;
            }
        }

        private void RotateZ(bool clockwise)
        {
            if (clockwise)
            {
                byte copy = Top;
                Top = Left;
                Left = Bottom;
                Bottom = Right;
                Right = copy;
            }
            else
            {
                byte copy = Top;
                Top = Right;
                Right = Bottom;
                Bottom = Left;
                Left = copy;
            }
        }

        public void Rotate(RotateAxis rotateAxis, bool clockwise)
        {
            if (rotateAxis == RotateAxis.X)
            {
                RotateX(clockwise);
            }
            else if (rotateAxis == RotateAxis.Y)
            {
                RotateY(clockwise);
            }
            else if (rotateAxis == RotateAxis.Z)
            {
                RotateZ(clockwise);
            }
        }
    }
}
